package com.ugoodtech.mdcc.core.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.QUserProfile;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.domain.UserProfile;
import com.ugoodtech.mdcc.core.dto.AdminUserVo;
import com.ugoodtech.mdcc.core.repository.UserProfileRepository;
import com.ugoodtech.mdcc.core.service.UserAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserAdminServiceImpl implements UserAdminService {

    @Autowired
    private UserProfileRepository userProfileRepository;


    /**
     * 获取管理员信息
     *
     * @return
     */
    @Override
    public AdminUserVo getAdminUser() {
        List<Object> userProfileIds = userProfileRepository.getUserByAuthoritieId(7L);
        return getUSer(userProfileIds);
    }

    private AdminUserVo getUSer(List<Object> userProfileIds) {
        List<Long> userIds = new ArrayList<>();
        for (Object userProfileId : userProfileIds) {
            BigInteger id = (BigInteger) userProfileId;
            userIds.add(id.longValue());
        }
        QUserProfile qUserProfile = QUserProfile.userProfile;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qUserProfile.deleted.eq(false));
        builder.and(qUserProfile.enabled.eq(true));
        builder.and(qUserProfile.user.id.in(userIds));
        List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(builder);
        List<User> userList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (UserProfile userProfile : userProfileList) {
            if (null != userProfile.getUser()) {
                userList.add(userProfile.getUser());
                stringBuilder.append(userProfile.getUser().getUsername() + ",");
            }
        }
        AdminUserVo adminUserVo = new AdminUserVo();
        adminUserVo.setTelephoneStr(stringBuilder);
        adminUserVo.setUserList(userList);
        return adminUserVo;
    }

}
